using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Slingshot : MonoBehaviour
{
    public static Slingshot instance;
    private LineRenderer leftlinerenderer;
    private LineRenderer rightlinerenderer;

    private Transform leftpoint;
    private Transform rightpoint;
    private Transform centerpoint;

    public bool isDrawing = false;
    private Transform birdTransform;
    private void Awake()
    {
        instance = this;
        leftlinerenderer = transform.Find("LeftLineRenderer").GetComponent<LineRenderer>();
        rightlinerenderer = transform.Find("RightLineRenderer").GetComponent<LineRenderer>();

        leftpoint = transform.Find("LeftPoint");
        rightpoint = transform.Find("Rightpoint");
        centerpoint = transform.Find("CenterPoint");
    }
    void Start()
    {
        
        HideLine();
    }

    // Update is called once per frame
    void Update()
    {
        if (isDrawing)
        {
            Draw();
        }
    }
    public void StartDraw(Transform birdTransform)
    {
        isDrawing = true;
        this.birdTransform = birdTransform;
        ShowLine();
    }
    public void EndDraw()
    {
        isDrawing = false;
        HideLine();
    }
    public void Draw()
    {
        Vector3 birdPosition = birdTransform.position;
        birdPosition = (birdPosition - centerpoint.position).normalized * 0.4f + birdPosition;

        leftlinerenderer.SetPosition(0, birdPosition);
        leftlinerenderer.SetPosition(1,leftpoint.position);

        rightlinerenderer.SetPosition(0, birdPosition);
        rightlinerenderer.SetPosition(1, rightpoint.position);
    }
    public Vector3 getCenterPositon()
    {
        return centerpoint.transform.position;
    }
    private void HideLine()
    {
        rightlinerenderer.enabled = false;
        leftlinerenderer.enabled = false;
    }
    private void ShowLine()
    {
        rightlinerenderer.enabled = true;
        leftlinerenderer.enabled = true;
    }
}
